﻿using Microsoft.Win32;
using NLua;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;

namespace HardToolApp.Tool
{
    static class EnumExtensions
    {
        public static string GetDescription(this Enum val)
        {
            var field = val.GetType().GetField(val.ToString());
            var customAttribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute));
            if (customAttribute == null) { return val.ToString(); }
            else { return ((DescriptionAttribute)customAttribute).Description; }
        }
    }

    internal class Common
    {
        public static byte Char2Byte(char c)
        {
            if(c <= '9' && c >= '0')
            {
                return (byte)(c - '0');
            }
            else if (c <= 'f' && c >= 'a')
            {
                return (byte)(c - 'a' + 10);
            }
            else if (c <= 'F' && c >= 'A')
            {
                return (byte)(c - 'a' + 10);
            }
            else
            {
                return 0;
            }
        }
        public static string Byte2String(byte[] b,char ch=' ',int len = 0)
        {
            StringBuilder sb = new StringBuilder();
            if(len == 0)
            {
                len = b.Length;
            }
            for(int i = 0; i < len;i++)
            {
                if (b[i] < 10)
                {
                    sb.Append('0');//不足补0
                }
                sb.Append(Convert.ToString(b[i],16));
                sb.Append(ch);
            }
            return sb.ToString();
        }

        public static byte[] String2Byte(string s)
        {
            int chLen = 0;
            if(string.IsNullOrEmpty(s))
            {
                return null;
            }
            List<byte> bytes = new List<byte>();
            byte b = 0;
            char ch = '\0';
            for (int i = 0; i < s.Length; i++)
            {
                ch = s.ElementAt(i);
                if(ch == ' ' || ch == ',' || ch == '，' || ch == ';')
                {
                    if(chLen != 0)
                    {
                        bytes.Add(b);
                    }
                    
                    chLen = 0;
                    b = 0;
                }
                else
                {
                    b *= 16;
                    b += Char2Byte(ch);
                    chLen++;
                    if(chLen >= 2)
                    {
                        bytes.Add(b);
                        chLen = 0;
                        b = 0;
                    }
                } 
            }
            if(chLen != 0)
            {
                bytes.Add(b);
                chLen = 0;
                b = 0;
            }
            return bytes.ToArray();
        }

        public static string OpenSaveDialog(string desc)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = desc;

            if (saveFileDialog.ShowDialog() == true)
            {
                //文件内容读取
                return saveFileDialog.FileName;
            }
            return null;
        }

        public static byte[] Lua2Byte(LuaTable t)
        {
            try
            {
                List<byte> bytes = new List<byte>();
                var iter = t.Values.GetEnumerator();
                while(iter.MoveNext())
                {
                    bytes.Add(Convert.ToByte( iter.Current));
                }
                return bytes.ToArray();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            return null;
        }

        /// <summary>
        /// 校验和
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static byte chksum(byte[] b)
        {
            byte sum = 0;
            for (int i = 0; i < b.Length - 1; i++)
            {
                sum += b[i];
            }
            return sum;
        }

        /// <summary>
        /// 校验和
        /// </summary>
        /// <param name="b"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static byte chksum(byte[] b, int len)
        {
            byte sum = 0;
            for (int i = 0; i < len; i++)
            {
                sum += b[i];
            }
            return sum;
        }

    }
}
